package initFunc

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
	"writerhelper/global"
	"writerhelper/model"
)

func InitializationDB()(*gorm.DB){
	dsn := "root:123456@tcp(127.0.0.1:3306)/writebooks?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Fatal("连接数据库出错：",err)
	}
	return db
}

//表的迁移
func AutoMigrate(db *gorm.DB){
	err := db.AutoMigrate(&model.User{},&model.Book{}, &model.Volume{},
	&model.Chapter{},&model.RoleSetting{})
	if err != nil {
		log.Fatal("表的迁移出现问题", err)
		return
	}
}

func Init(){
	global.MysqlDB = InitializationDB()
	AutoMigrate(global.MysqlDB)

}